﻿using System;
using System.Runtime.CompilerServices;
using Core.Base.Module;
using Core.Extension;
using DG.Tweening;
using UnityEngine;

namespace Core.Mgr._EventSys
{
    public static class EventExtension
    {
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="mono">生命周期所绑定的对象，当前者Destroy时，注册的事件会自动注销</param>
        /// <param name="notification"></param>
        /// <param name="action"></param>
        /// <param name="bindObj">绑定的对象。若为null。则绑定到mono上。否则绑定到bindObj</param>
        public static void RegHandler(this MonoBehaviour bindObj, Enum notification, Action<ITuple> action)
        {
            EventMgr.RegHandler(notification, action);
            //绑定自动销毁功能
            bindObj.gameObject.OnDestroy(delegate { EventMgr.UnRegHandler(notification, action); });
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="notification"></param>
        /// <param name="action"></param>
        /// <param name="bindModule">事件所绑定的模块。当模块卸载,会自动注销所注册事件</param>
        public static void RegHandler(this BaseModule bindModule, Enum notification, Action<ITuple> action)
        {
            EventMgr.RegHandler(notification, action);
            //绑定自动销毁功能
            if (bindModule != null)
                bindModule.onUnload += delegate { EventMgr.UnRegHandler(notification, action); };
        }
    }
}